// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 2e5+50;

char t[N];

char *s[N];
LL n;
int m;
LL women, men;

LL maxp;
LL k[N];
int l[N];

int pre[N];

int main(int argc, char const *argv[]) {
    // code
    scanf("%lld%d", &n, &m);
    // w = m = 0;
    maxp = 0;
    LL cur = 0;
    for (int i=0; i<m; ++i) {
        scanf("%s%lld", t, &k[i]);
        int len = strlen(t);
        pre[0] = 0;
        int maxs = 0;
        for (int j=0; j<len; ++j) {
            int del = t[j] == 'M' ? -1:1;
            if (del == -1)
                men += k[i];
            else
                women += k[i];
            pre[j+1] = pre[j] + del;
            maxs = max(maxs, pre[j+1]);
        }
        LL tans = cur + max(0LL, (k[i]-1) * pre[len]) + maxs;
        cur += k[i] * pre[len];
        l[i] = len;

        maxp = max(maxp, tans);

    }
    if (women < men) {
        puts("-1");
        return 0;
    }
    // dbg(maxp, n, women, men);
    LL ans = maxp - (n*2-men*2) - 1;
    ans = max(ans, 0LL);
    cout << ans << endl;
    return 0;
}
